package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.RoleEntity;
import com.sir.tao.carhire.service.RoleService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author tao
 * @data 2021/12/6 10:48
 * @description: 管理员登录接口
 */
@RestController
@Api(tags = "角色管理接口")
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 分页列表查询
     *
     */
    @ApiOperation("分页列表查询")
    @ApiImplicitParam(name = "分页列表查询", value = "分页列表查询")
    @GetMapping(value = "/list")
    @RequiresRoles("超级管理员")
    public CommonResult<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                         String search) {

        List<RoleEntity> list = roleService.list(new LambdaQueryWrapper<RoleEntity>()
                .like(!StringUtils.isEmpty(search),RoleEntity::getRoleName,search)
                .or()
                .like(!StringUtils.isEmpty(search),RoleEntity::getRoleCode,search)
        );
        PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
        return CommonResult.success(pageUtil);
    }

    /**
     * 添加
     *
     */
    @ApiOperation(value="角色-添加", notes="角色-添加")
    @PostMapping(value = "/add")
    public CommonResult<?> add(@RequestBody RoleEntity role) {
        roleService.save(role);
        return CommonResult.success("添加成功！");
    }

    /**
     * 编辑
     *
     */
    @ApiOperation(value="角色-编辑", notes="角色-编辑")
    @PutMapping(value = "/edit")
    public CommonResult<?> edit(@RequestBody RoleEntity role) {
        roleService.updateById(role);
        return CommonResult.success("编辑成功!");
    }

    /**
     * 通过id删除
     *
     */
    @ApiOperation(value="角色-通过id删除", notes="角色-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name="id") String id) {
        roleService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     */
    @ApiOperation(value="角色-批量删除", notes="角色-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@RequestParam(name="ids") String ids) {
        this.roleService.removeByIds(Arrays.asList(ids.split(",")));
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     */
    @ApiOperation(value="角色-通过id查询", notes="角色-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id") String id) {
        RoleEntity role = roleService.getById(id);
        return CommonResult.success(role);
    }



}
